package com.jiayang.hhotel.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.bean.RoomThingModel;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/5/16 0016.
 * 客房物品适配器
 */

public class RoomThingListAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<RoomThingModel.PHKTransListDtoBean> mList;
    private HashMap<Integer,Integer> countMap;

    public RoomThingListAdapter(Context context, List<RoomThingModel.PHKTransListDtoBean> list){
        this.inflater=LayoutInflater.from(context);
        this.mList=list;
        this.countMap=new HashMap<>();
        for(int i=0;i<mList.size();i++){
            countMap.put(i,0);
        }
    }

    public void initMap(){
        for(int i=0;i<mList.size();i++){
            countMap.put(i,0);
        }
        this.notifyDataSetChanged();
    }

    public HashMap<Integer,Integer> getCountMap(){
        return countMap;
    }

    public List<RoomThingModel.PHKTransListDtoBean> getmList(){
        return mList;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        RoomThingModel.PHKTransListDtoBean model=mList.get(position);
        if(convertView==null){
            convertView=inflater.inflate(R.layout.room_thing_list_item,null);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder= (ViewHolder) convertView.getTag();
        holder.name.setText(model.getHKMenuDescp());
        holder.price.setText(model.getPrice()+"");
        holder.count.setText(model.getQuantity1()+"");
        holder.currentCount.setText(countMap.get(position)+"");
        final ViewHolder finalHolder = holder;
        holder.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int Count=countMap.get(position)+1;
                countMap.put(position,Count);
                finalHolder.currentCount.setText(Count+"");
            }
        });
        holder.reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int Count=countMap.get(position)-1;
                countMap.put(position,Count);
                finalHolder.currentCount.setText(Count+"");
            }
        });
        return convertView;
    }

    class ViewHolder{
        TextView name;//名称
        TextView price;//价格
        TextView count;//定量
        ImageView reduce;//减
        ImageView add;//加
        TextView currentCount;//选择的数量

        ViewHolder(View itemView){
            this.name= (TextView) itemView.findViewById(R.id.tv_name);
            this.price= (TextView) itemView.findViewById(R.id.tv_price);
            this.count= (TextView) itemView.findViewById(R.id.tv_nation);
            this.reduce= (ImageView) itemView.findViewById(R.id.iv_reduce);
            this.add= (ImageView) itemView.findViewById(R.id.iv_add);
            this.currentCount= (TextView) itemView.findViewById(R.id.tv_count);
        }
    }
}
